package com.example.ui.learn;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.learn.util.CurriculumCenter;
import com.example.ui.learn.util.MyListViewAdapter1;
import com.example.ui.learn.util.MyListViewAdapter5;
import com.example.ui.learn.util.Recommend;

import com.example.util.AsyncLoadImage;
import com.example.util.RefreshableListView;
import com.example.util.RefreshableListView.OnRefreshListener;
import com.google.gson.JsonObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RecommendActivity extends Activity {
	private Button ra_topleft, ra_topright;
	private TextView ra_toptitle;
	private Button ra_activity, ra_course;
	private ImageView ra_img1, ra_img2;
	private RefreshableListView mListView;
	private String data;
	private ArrayList<Recommend> ra_list = new ArrayList<Recommend>();;
	private MyListViewAdapter5 ra_adapter;
	private Intent i;
	private String tid,title,name;//教师 id
	private String result;
	private int type =1;//0course 1activity
	AlertDialog infoad;
	AlertDialog.Builder bulider;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_recommend);
		i = getIntent();
		tid = i.getStringExtra("blId");
		title=i.getStringExtra("title");
		name=i.getStringExtra("name");
		init();
	}
	private void init() {
		bulider = new AlertDialog.Builder(RecommendActivity.this);
		bulider.setView(View.inflate(RecommendActivity.this, R.layout.onloading, null))
				.create();
		infoad = bulider.show();
		
		ra_topleft = (Button) findViewById(R.id.layout_title_left_btn);
		ra_topright = (Button) findViewById(R.id.layout_title_right_btn);
		ra_toptitle = (TextView) findViewById(R.id.layout_title_middle_text);

		ra_activity = (Button) findViewById(R.id.ar_activity);
		ra_course = (Button) findViewById(R.id.ar_course);
		ra_topleft.setText("返回");
		ra_topright.setText("邀请讲师");
		Drawable d = getResources().getDrawable(R.drawable.al_topright);
		d.setBounds(0, 0, 21, 19);
		ra_toptitle.setText("推荐");

		mListView = (RefreshableListView) findViewById(R.id.item01_listview);
		ra_adapter = new MyListViewAdapter5(this, ra_list, new AsyncLoadImage());
		mListView.setAdapter(ra_adapter);
		new Thread(ra_run).start();
	    
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				String id = ra_list.get(position).getId();
				Intent i = new Intent();
				i.setClass(RecommendActivity.this, DetailActivity.class);
				i.putExtra("id", id);
				i.putExtra("type", type);
				startActivity(i);
			}

		});
		mListView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(RefreshableListView listView) {
				new NewDataTask().execute();

			}
		});

		ra_topleft.setOnClickListener(ra_ocl);
		ra_topright.setOnClickListener(ra_ocl);
		ra_activity.setOnClickListener(ra_ocl);
		ra_course.setOnClickListener(ra_ocl);
	}
	
	private Runnable ra_run = new Runnable() {

		@Override
		public void run() {
			data = NetRequest.Request_Recommend(tid,type);
			parsingdata(); 
		}
	};
	private void parsingdata() {
		JSONObject j;
		try {
			j = new JSONObject(data);
			result = j.getString("response");
			if (result.equals("true")) {
				ra_list = AnalyzingJson.getdata_ra(data);
				h.sendEmptyMessage(1);
			} else {
				h.sendEmptyMessage(2);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				ra_adapter.setCcInfos(ra_list);
				ra_adapter.notifyDataSetChanged();
				infoad.dismiss();
				break;
			case 2:
				infoad.dismiss();
			default:
				break;
			}

		};
	};

	private class NewDataTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {

			new Thread(ra_run).start();
			mListView.completeRefreshing();

			super.onPostExecute(result);
		}
	}

	private OnClickListener ra_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ar_activity:
				type = 1;
				new Thread(ra_run).start();
				break;
			case R.id.ar_course:
				type = 0;
				new Thread(ra_run).start();
				break;
			case R.id.layout_title_left_btn:
				finish();
				break;
			case R.id.layout_title_right_btn:
				Log.i("...", title+"/"+name);
				Intent cca_l = new Intent();
				cca_l.putExtra("tid",tid);
				cca_l.putExtra("title", title);
				cca_l.putExtra("name", name);
				cca_l.setClass(RecommendActivity.this, InviteActivity.class);
				startActivity(cca_l);
				break;
			default:
				break;
			}
		}
	};

}
